package com.booter.ws.session;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator;

import java.io.IOException;
import java.security.Principal;

/**
 * @author lcw
 * @date 2022/6/17 10:05
 */
public class CustomWebSocketSessionDecorator extends ConcurrentWebSocketSessionDecorator {

    protected CustomSubProtocolWebSocketHandler webSocketHandler;

    protected Principal user;

    //NIO ByteBuffer类来接收数据，ByteBuffer使用了堆外内存
    //MaxTextMessageBufferSize设置过大后，影响了堆外内存，会造成客户端的断开
    public CustomWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit, CustomSubProtocolWebSocketHandler webSocketHandler) {
        super(delegate, sendTimeLimit, bufferSizeLimit);
        this.webSocketHandler = webSocketHandler;
    }

    public CustomWebSocketSessionDecorator(WebSocketSession delegate, int sendTimeLimit, int bufferSizeLimit, OverflowStrategy overflowStrategy, CustomSubProtocolWebSocketHandler webSocketHandler) {
        super(delegate, sendTimeLimit, bufferSizeLimit, overflowStrategy);
        this.webSocketHandler = webSocketHandler;
    }

    public void setPrincipal(Principal user) {
        this.user = user;
    }

    @Override
    public Principal getPrincipal() {
        return this.user;
    }

    @Override
    public void close() throws IOException {
        this.close(CloseStatus.GOING_AWAY);
    }

    @Override
    public void close(CloseStatus status) throws IOException {
        super.close(status);
    }

}
